﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;

namespace DCS7GUI
{
    public partial class Helpers
    {
        public static string BytesToHex(byte[] bytes)
        {
            string result = "";
            foreach (byte b in bytes)
            {
                result += "0123456789ABCDEF".Substring(b / 16, 1);
                result += "0123456789ABCDEF".Substring(b % 16, 1);
            }
            return result;
        }

        public static byte[] HexToBytes(string Hex)
        {
            List<byte> results = new List<byte>();
            byte currentValue = 0;
            bool nibble = false;
            foreach (char c in Hex)
            {
                if (!nibble)
                {
                    currentValue += (byte)("0123456789ABCDEF".IndexOf(c) * 16);
                }
                else
                {
                    currentValue += (byte)("0123456789ABCDEF".IndexOf(c));
                    results.Add(currentValue);
                    currentValue = 0;
                }
                nibble = !nibble;
            }
            return results.ToArray();
        }

        public static Bitmap DrawRectangle(int x, int y, int width, int height, Bitmap image)
        {
            for (int X = x * 4; X - (x * 4) < width * 4; X++)
                for (int Y = y * 4; Y - (y * 4) < height * 4; Y++)
                    image.SetPixel(X, Y, Color.Black);
            return image;
        }

        public static Bitmap ClearRectangle(int x, int y, int width, int height, Bitmap image)
        {
            for (int X = x * 4; X - (x * 4) < width * 4; X++)
                for (int Y = y * 4; Y - (y * 4) < height * 4; Y++)
                    image.SetPixel(X, Y, Color.White);
            return image;
        }

        public static Bitmap PixelOn(int X, int Y, Bitmap image)
        {
            DrawRectangle(X, Y, 1, 1, image);
            return image;
        }

        public static Bitmap PixelOff(int X, int Y, Bitmap image)
        {
            ClearRectangle(X, Y, 1, 1, image);
            return image;
        }

        public static Bitmap EmptyRectangle(int X, int Y, int Width, int Height, Bitmap image)
        {
            DrawRectangle(X, Y, 1, Y + Height, image);
            DrawRectangle(X, Y, X + Width, 1, image);
            DrawRectangle(X + Width - 1, Y, 1, Y + Height, image);
            DrawRectangle(X, Y + Height - 1, X + Width, 1, image);
            return image;
        }

        public static Bitmap DrawSprite(int X, int Y, byte[] Sprite, Bitmap Image)
        {
            for (int row = 0; row < Sprite.Length; row++)
            {
                BitArray bits = new BitArray(new byte[] { Sprite[row] }); // Get the bits
                int tempX = X;
                for (int i = bits.Count - 1; i != 0; i--)
                {
                    try
                    {
                        if (Image.GetPixel(X * 4, Y * 4).R != 0)
                        {
                            if (bits[i])
                                DrawRectangle(X, Y, 1, 1, Image);
                        }
                        else
                        {
                            if (bits[i])
                                ClearRectangle(X, Y, 1, 1, Image);
                        }
                    }
                    catch { }
                    X++;
                }
                X = tempX;
                Y++;
            }
            return Image;
        }

        public static int TextWidth(string Text)
        {
            int width = 0;
            foreach (char c in Text)
                width += CharacterSet[((byte)c - 0x20) * 6 + 5];
            return width;
        }

        public static Bitmap DrawText(int X, int Y, string Text, Bitmap Image)
        {
            foreach (char c in Text)
            {
                int tempY = Y;
                for (int row = 0; row < 5; row++)
                {
                    BitArray bits = new BitArray(new byte[] { CharacterSet[((byte)c - 0x20) * 6 + row] }); // Get the bits
                    int tempX = X;
                    for (int i = bits.Count - 1; i != 0; i--)
                    {
                        try
                        {
                            if (Image.GetPixel(X * 4, Y * 4).R != 0)
                            {
                                if (bits[i])
                                    DrawRectangle(X, Y, 1, 1, Image);
                            }
                            else
                            {
                                if (bits[i])
                                    ClearRectangle(X, Y, 1, 1, Image);
                            }
                        }
                        catch { }
                        X++;
                    }
                    X = tempX;
                    Y++;
                }
                X += CharacterSet[((byte)c - 0x20) * 6 + 5];
                Y = tempY;
            }
            return Image;
        }
    }
}
